import Vue from "vue";
import Router from "vue-router";
import head from "../pages/cart/head.vue";
import items from "../pages/cart/items.vue";
import details from "../pages/cart/details.vue";
import cart from "../pages/cart/cart.vue";
import close from "../pages/cart/Close";
import Balance from "../pages/Return/Balance.vue";
import ReturnGoods from "../pages/Return/ReturnGoods.vue";
import myReturn from "../pages/Return/MyReturn.vue";
import order from "../pages/manage/Order.vue";
import Count from "../pages/manage/Count";
import item from "../pages/ItemMain/item";
import login from "../pages/user/login.vue";
import register from "../pages/user/register.vue";
import address from "../pages/user/address.vue";
import UserInfo from "../pages/user/UserInfo.vue";
import showAddress from "../pages/user/showAddress.vue";
import Manage from "../pages/manage/Manage.vue";
import test from "../pages/manage/test.vue";
import {localTake} from "../service/index";
import {getSession} from "../service/user";

Vue.use(Router);
const routes = [
    {path: "/head", component: head},
    {
        path: "/items",
        component: items,
        meta: {
            keepAlive: true, // 需要被缓存
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/Manage",
        component: Manage,
        children: [{path: "/manage/test", component: test},
            {path: "/manage/Count", component: Count},
            {
                path: "/manage/items",
                component: item,
                meta: {
                    needLogin: true, // 需要登录
                },
            },],
        meta: {
            keepAlive: true, // 需要被缓存
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/cart",
        component: cart,
        meta: {
            keepAlive: true, // 需要被缓存
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/details",
        component: details,
        meta: {
            keepAlive: true,
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/Balance",
        component: Balance,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/returnGoods",
        component: ReturnGoods,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/myReturn",
        component: myReturn,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/order",
        component: order,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/count",
        component: Count,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/close",
        component: close,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {path: "/login", component: login},
    {path: "/register", component: register},
    {
        path: "/address",
        component: address,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/showAddress",
        component: showAddress,
        meta: {
            keepAlive: true, // 需要被缓存
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/userInfo",
        component: UserInfo,
        meta: {
            needLogin: true, // 需要登录
        },
    },
    {
        path: "/",
        component: login,
    },
];

const router = new Router({
    mode: "history",
    base:'/front/',
    routes,
});

// const originalPush = Router.prototype.push;

// Router.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch((err) => err);
// };
// 全局路由守卫
router.beforeEach(async (to, from, next) => {
    await getSession().then((response) => {
        let isLogin = localTake(response);
        if (to.meta.needLogin) {
            // 判断该路由是否需要登录权限
            if (isLogin) {
                // 判断是否已经登录
                // console.log("已登录 " + to.path);
                next((vm) => {
                    vm.$router.push(to.path);
                });
                // next()
            } else {
                // console.log("未登录，跳转登录");
                alert("请先登录账号");
                next({
                    path: "/login",
                    query: {redirect: to.fullPath}, // 将跳转的路由path作为参数，登录成功后跳转到该路由
                });
            }
        } else {
            // console.log("不用登录");
            next();
        }
    });
});

export default router;
